package com.houhanzhi.controller;


import com.houhanzhi.controller.request.TrafficPageRequest;
import com.houhanzhi.controller.request.UseTrafficRequest;
import com.houhanzhi.enums.BizCodeEnum;
import com.houhanzhi.response.JsonData;
import com.houhanzhi.service.TrafficService;
import com.houhanzhi.vo.TrafficVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author houhanzhi
 * @since 2025-01-02
 */
@RestController
@RequestMapping("/api/traffic/v1")
public class TrafficController {

    @Autowired
    private TrafficService trafficService;

    @Value("${rpc.token}")
    private String rpcToken;


    /**
     *
     *  扣减流量包API
     * @param useTrafficRequest
     * @param request
     * @return
     */
    @PostMapping("/reduce")
    public JsonData useTraffic(@RequestBody UseTrafficRequest useTrafficRequest, HttpServletRequest request){
        final String requestRpcToken = request.getHeader("rpc-token");
        if(rpcToken.equalsIgnoreCase(requestRpcToken)) {
            // 具体使用流量包逻辑
            return trafficService.reduce(useTrafficRequest);
        }
        return JsonData.buildError("非法访问");
    }

    /**
     * 分页查询可用流量包
     * @param request
     * @return
     */
    @PostMapping("/page")
    public JsonData pageOfAvailableTraffic(@RequestBody TrafficPageRequest request) {
        return trafficService.pageOfAvailableTraffic(request);
    }

    /**
     * 流量包详情
     *
     * @return
     */
    @GetMapping("/detail/{trafficId}")
    public JsonData detail(@PathVariable("trafficId") Long trafficId) {
        TrafficVO trafficVO = trafficService.detail(trafficId);
        return JsonData.buildSuccess(trafficVO);
    }
}

